package Java8.map;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;

public class MapMergeTest {


  public static void main(String[] args) {
    Map<Integer, String> map = new HashMap<>();
    map.put(1, "1111111");
    map.put(2, "222");

    map.merge(1, "111==333", (existValue, newValue) -> existValue + newValue);
    map.merge(4, "111==4444", (a, b) -> a + b);

    System.out.println(map.get(1));
    System.out.println(map.get(4));

    System.out.println("===========");
    testStudent();
  }


  public static void testStudent() {

    System.out.println((Object) null);

    System.out.println("nulllllll");
    //学生的集合
    List<Student> students = new ArrayList<>();
    students.add(new Student("张三", "男", 18));
    students.add(new Student("李四", "男", 20));
    students.add(new Student("韩梅梅", "女", 18));
    students.add(new Student("小红", "女", 45));

      //声明接收结果的 map
      Map<String, Integer> resultMap = new HashMap<>();
      for (Student student : students) {
          resultMap.merge(student.getSex(), student.getAge(), (a, b) -> b + a);
      }
      printResult(resultMap);

      SmartInstantiationAwareBeanPostProcessor s;

      Optional<Integer> max = students.stream().map(Student::getAge).reduce(Integer::max);
      if (max.isPresent()) {
          System.out.println(max.get());
      }
  }


  private static void printResult(Map<String, Integer> map) {
    for (Map.Entry<String, Integer> e : map.entrySet()) {
      System.out.println("key:" + e.getKey() + "   " + "value:" + e.getValue());
    }
  }

}

class Student {

  private String name;
  private String sex;
  private int age;

  public Student(String name, String sex, int age) {
    this.name = name;
    this.sex = sex;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }
}